<template>
<div>
    <Form :model="formCustom" :label-width="80"> 
        <FormItem label="名称" prop="topic">
            <Input type="text" v-model="formCustom.topic" ></Input>
        </FormItem>
        <FormItem label="类别" prop="t">
            <Select v-model="formCustom.t">
                <Option :value="1">享堂集市</Option>
                <Option :value="2">享堂学院</Option>
                <Option :value="3">享堂书院</Option>
            </Select> 
        </FormItem>
        <FormItem label="图片" prop="head" >
            <Upload action="//upload.qiniup.com/" 
             :data="token"
            :format="['jpg','jpeg','png']" 
            :on-success="suc">
                <img :src="formCustom.head" :hidden="hide">
                <Button type="ghost" icon="ios-cloud-upload-outline">上传图片</Button>
            </Upload>
        </FormItem>
        <FormItem label="链接" prop="redirect_url">
            <Input type="text" v-model="formCustom.redirect_url" ></Input>
        </FormItem>
        <FormItem label="排序" prop="sort">
            <Input type="text" v-model="formCustom.sort" ></Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit">Submit</Button>
        </FormItem>
    </Form>
</div>
</template>
<script>
    import util from '../libs/util';
    export default {

        created() {
            this.formCustom.head = "";
           this.loadUpToken();
        },
        
        data () {
            return {
                formCustom: {},
                hide : true,
                token: {}
            }
        },
        methods: {
            suc(res, file) {
// 上传成功，图片赋值
                this.hide = false;
                this.formCustom.head = res.baseUrl+"/"+res.key;
            },
             // 加载上传token
            loadUpToken() {
                this.$http.get(util.baseURL+"qiniu/getUpToken",
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                               this.token = {
                                   token : response.data.data
                               }
                            } 
                        },
                        (error)=>{
                            this.$Message.error("更新失败！");
                        }
                );
                this.token = {
                    token:''
                }
            },
            handleSubmit () {
                //this.formCustom.head = "zjl",
                this.$http.post(util.baseURL+"ads/addAds",this.formCustom,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                               this.$Message.info("添加成功！");
                               this.$router.push("/index/advertisement");
                               this.formCustom = {};
                            } else {
                               this.$Message.error("添加失败！");
                            }
                        },
                        (error)=>{
                            this.$Message.error("添加失败！");
                        }
                );
            },
        }
    }
</script>